/**
 * Created by x on 17-7-28.
 */
angular.module('tribalc').controller('AddrentAgreementController',
    ['$scope', '$rootScope', '$stateParams', 'Api', 'Enum', 'Alert', '$state', 'RentProtocolModel', 'dataPicker', function ($scope, $rootScope, $stateParams, Api, Enum, Alert, $state, RentProtocolModel, dataPicker) {

        $scope.additionalRentInfo = new RentProtocolModel();
        $scope.upload = {pictures: []};

        $scope.submitAddrentinfo = submitAddrentinfo;
        // $scope.checkCountPayCycle=checkCountPayCycle;
        // $scope.checkCountMonthlyRent=checkCountMonthlyRent;
        $scope.uploadRentPic = uploadRentPic;
        $("#endTime").selectDate();
        $("#startTime").selectDate()

        function getComunityIdUrl() {
            return 'communitys/' + $rootScope.RootCompanyInfo.communityId;
        }

        function uploadRentPic(ele) {
            var $divEle = $(ele).parent();
            // console.log($divEle)
            var file = ele.files[0];
            var comunityIdUrl = getComunityIdUrl()
            var index = $(".upload-rent-pic").index($($divEle).parent());
            setupPictures($($divEle), file);
            uploadPicture(file, comunityIdUrl, function (imgeUrl) {
                // console.log(imgeUrl);
                $scope.$apply(function () {
                    $scope.upload.pictures[index] = imgeUrl;
                });
            });
        }

       var uploadNumber = 0

        function uploadPicture(file, comunityIdUrl, sucFuc) {
            Api.uploadPicture(hex_md5(file), comunityIdUrl, function (result) {
                Api.callAliyunPicture(result.data.objectKey, result.data.url, file, function (result) {
                    sucFuc(result);
                }, function () {
                    uploadNumber--
                });
            }, function () {
                uploadNumber--

            });
        }

        function setupPictures($divEle, file) {
            var fileReader = new FileReader();
            fileReader.onload = function () {
                var imgSrc = fileReader.result;
                if (imgSrc) {
                    $divEle.find('img').attr("src", imgSrc);
                    $divEle.addClass("pro-logo-completed-1");

                }
            };
            fileReader.readAsDataURL(file);
        }

        $scope.rentpaycycles = {
            num: Enum.rentPayCycle
        };

        function submitAddrentinfo(rentinfo) {

            rentinfo.beginTime = getTime($("#startTime"))
            rentinfo.endTime = getTime($('#endTime'))
            // rentinfo.pictures = $scope.upload.pictures;
            rentinfo.type = "APARTMENT";
            rentinfo.sourceId = $stateParams.apartmentId;
            rentinfo.pictures = handleImageUrl($scope.upload.pictures);
            rentinfo.communityId = $rootScope.RootCompanyInfo.communityId;
            Api.createRentProtocol(rentinfo, function (result) {
                console.log(result)
                $scope.additionalRentInfo = new RentProtocolModel();

                if(result.code===201){
                    $state.go('company.companyRentagreementDetail',{apartmentId:$stateParams.apartmentId});
                }
            });
        }

        function handleImageUrl(){
            var pictures = [];
            for(var i =0;i<$scope.upload.pictures.length;i++){
                pictures.push($scope.upload.pictures[i].replace('https://buluo-gs-pictures.oss-cn-beijing.aliyuncs.com/','oss://'));
            }
            return pictures;
        }


        function getTime(element) {
            var year = element.find('select[name=year]').find('option:selected').val();
            var month = element.find('select[name=month]').find('option:selected').val();
            var day = element.find('select[name=days]').find('option:selected').val();
            if (!year || !month || !day) {
                return null;
            }
            return year + '-' + month + '-' + day;
        }


    }]);